/*
 *  Copyright 2020-2021 Li Fei
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.pengshun.modules.api.coal.controller;

import com.pengshun.modules.api.coal.domain.UserAddress;
import com.pengshun.modules.api.coal.service.UserAddressService;
import com.pengshun.modules.api.coal.service.dto.UserAddressDto;
import com.pengshun.modules.security.Authorization;
import com.pengshun.modules.security.CurrUserInfo;
import com.pengshun.modules.domain.User;
import com.pengshun.modules.common.BaseResult;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import springfox.documentation.annotations.ApiIgnore;


/**
 * @author lifei
 * @date 2020-11-13
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "用户地址管理")
@RequestMapping("/api/userAddress")
@ApiIgnore
public class UserAddressController {

    private final UserAddressService userAddressService;


    @GetMapping(value = "/list", produces = "application/json; charset=utf-8")
    @ApiOperation("查询用户地址")
    @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    @Authorization
    public BaseResult query(@ApiIgnore @CurrUserInfo User user) {
        return userAddressService.queryAll(user.getId());
    }

    @PostMapping(value = "/save", produces = "application/json; charset=utf-8")
    @ApiOperation("新增用户地址")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    public BaseResult add(@ApiIgnore @CurrUserInfo User user, UserAddress address) {
        address.setUserId(user.getId());
        return userAddressService.save(address);
    }

    @PostMapping(value = "/update", produces = "application/json; charset=utf-8")
    @ApiOperation("修改用户地址")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header"),
            @ApiImplicitParam(name = "userAddressId", value = "地址id", dataType = "string", required = true, paramType = "query")
    })
    public BaseResult update(@ApiIgnore @CurrUserInfo User user, UserAddress address) {
        address.setUserId(user.getId());
        return userAddressService.update(address);
    }
    @PostMapping(value = "/delete", produces = "application/json; charset=utf-8")
    @ApiOperation("删除用户地址")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header"),
            @ApiImplicitParam(name = "userAddressId", value = "地址id", dataType = "string", required = true, paramType = "query")
    })
    public BaseResult delete(@ApiIgnore @CurrUserInfo User user, UserAddress address) {
        address.setUserId(user.getId());
        return userAddressService.delete(address);
    }

    @ApiOperation("查询用户默认地址")
    @GetMapping(value = "/defaultAddress", produces = "application/json; charset=utf-8")
    @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    @Authorization
    public BaseResult defaultAddress(@ApiIgnore @CurrUserInfo User user) {
        return userAddressService.defaultAddress(user.getId());
    }

    @ApiOperation("根据id查询详情")
    @GetMapping(value = "/detil", produces = "application/json; charset=utf-8")
    @ApiImplicitParam(name = "userAddressId", value = "地址id", dataType = "string", required = true, paramType = "query")
    @Authorization
    public BaseResult detil(@ApiIgnore UserAddress address) {
        return userAddressService.findById(address.getUserAddressId());
    }

    @ApiOperation(value = "查询历史搜索")
    @GetMapping(value = "/history-city")
    @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    @Authorization
    public BaseResult selectHistoryCity(@ApiIgnore @CurrUserInfo User user ) {
        BaseResult res = userAddressService.selectHistoryCity(user.getId());
        return res;
    }

    @ApiOperation(value = "保存历史搜索")
    @PostMapping(value = "/save-history-city")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cityId", value = "城市id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @Authorization
    public BaseResult saveHistoryCity(@ApiIgnore @CurrUserInfo User user, UserAddressDto para) {
        para.setUserId(user.getId());
        BaseResult res = userAddressService.saveHistoryCity(para);
        return res;
    }
    @ApiOperation(value = "删除历史搜索")
    @PostMapping(value = "/del-history-city")
    @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    @Authorization
    public BaseResult delHistoryCity(@ApiIgnore @CurrUserInfo User user) {
        BaseResult res = userAddressService.delHistoryCity(user.getId());
        return res;
    }
}